package com.ycy.doors;


import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo01 {
    
    public static String getAttributeValue(Element cur) {
        String res = null;
        for (Element e1 : cur.elements()) {
            if ("div".equals(e1.getName())) {
                res = e1.getTextTrim(); break;
            }
            res = getAttributeValue(e1);
        }
        
        return res;
    }
    
    // 获取属性ref
    public static String getAttributeRef(Element cur, String type) {
        String res = "";
        for (Element e : cur.elements()) {
            String textTrim = e.getTextTrim();
            /* 先看当前节点有没有找到 */
            if ("heading".equals(type)) {
                if (textTrim.contains("NAME-DOORS-MODULE") || textTrim.contains("OBJECTHEADING")) {
                    res = textTrim; break;
                }
            }
            else if ("text".equals(type)) {
                if (textTrim.contains("OBJECTTEXT")) {
                    res = textTrim; break;
                }
            }
            
            /* 递归子节点找到后就不再找了 */
            String temp = getAttributeRef(e, type);
            if (!"".equals(temp)) {
                res = temp; break;
            }
        }
        
        return res;
    }
    
    /* 获取node所需的值 */
    public static String getTitleValue(Element cur) {
        StringBuilder res = new StringBuilder();
        Element valuesElement = null;
        for (Element e1 : cur.elements()) {
            if ("VALUES".equals(e1.getName())) {
                valuesElement = e1;
            }
        }
    
        if (valuesElement != null) {
            for (Element e2 : valuesElement.elements()) {
                String headingAttributeRef = getAttributeRef(e2, "heading");
                if (!"".equals(headingAttributeRef)) {
                    res = new StringBuilder(getAttributeValue(e2));
                }
    
                String textAttributeRef = getAttributeRef(e2, "text");
                if (!"".equals(textAttributeRef)) {
                    res.append(":").append(getAttributeValue(e2));
                }
            }
        }
        
        return res.toString();
    }
    
    public static void build(Element cur, Map<String, Element> map, List<Element> list) {
        String name = cur.getName(); // label name
        String identifier = cur.attributeValue("IDENTIFIER"); // 获取identifier属性值
        List<Element> elements = cur.elements(); // 子标签元素
        
        if (identifier != null) {
            map.put(identifier, cur);
        }
        
        if ("SPECIFICATION".equals(name)) {
            list.add(cur);
        }
    
        for (Element e : elements) {
            build(e, map, list);
        }
    }
    
    /* 获取children */
    public static List<Element> getChildren(Element cur) {
        Element childrenElement = cur.element("CHILDREN");
        if (childrenElement == null) {
            return new ArrayList<>();
        }
        else {
            return childrenElement.elements();
        }
    }
    
    /* 构建node，用于查看 */
    public static Node buildNode(Element cur, List<Element> children, Map<String, Element> map) {
        if (cur == null) {
            return null;
        }
        
        String contentValue = getTitleValue(cur);
        String titleValue;
        String textValue = "";
        if (contentValue.contains(":")) {
            String[] split = contentValue.split(":");
            titleValue = split[0];
            textValue = split[1];
        }
        else {
            titleValue = contentValue;
        }
        
        Node node = new Node(cur.attributeValue("IDENTIFIER"), titleValue, textValue);
    
        for(Element child : children) {
            String objectRefString = child.element("OBJECT").elementTextTrim("SPEC-OBJECT-REF"); // 找出ref
            List<Element> subChildren = getChildren(child); // 找出children
            Element e = map.get(objectRefString); // 通过ref找出具体的元素
            
            Node subNode = buildNode(e, subChildren, map);
            if (subNode != null) node.addChild(subNode);
        }
        
        return node;
    }
    
    public static void printNode(Integer level, Node node, FileWriter writer) throws IOException {
        if (!"".equals(node.getTitle()) || !"".equals(node.getText())) {
            /* 向文件中写入内容 */
            for (int i = 0; i < level; i++) {
                writer.write("\t");
            }
            writer.write(node.getTitle() + ":" + node.getText() + "\n");
            writer.flush();
        }
        
        for (Node sub : node.getChildren()) {
            printNode(level + 1, sub, writer);
        }
    }
    
    public static void main(String[] args) throws DocumentException {
        // 1.创建Reader对象
        SAXReader reader = new SAXReader();
        // 2.加载xml
        Document document = reader.read(new File("D:\\DownloadingFiles\\解析\\GS直升机传动系统.reqif"));
        // 3.获取根节点
        Element rootElement = document.getRootElement();
    
        Map<String, Element> map = new HashMap<>();
        List<Element> list = new ArrayList<>();
        List<Node> nodeList = new ArrayList<>();
    
        build(rootElement, map, list); // 先构建
        
        /* 构建node */
        for(Element e : list) {
            List<Element> children = getChildren(e); // 找出children
            Node node = buildNode(e, children, map);
            nodeList.add(node);
        }
    
        try {
            File file = new File("D:\\DownloadingFiles\\parseOut.txt");
            boolean canWrite = file.exists();
            if (!canWrite) {
                canWrite = file.createNewFile(); // 如果文件不存在，创建文件
            }
            
            if (canWrite) {
                FileWriter writer = new FileWriter(file, true); // 创建FileWriter对象
    
    
                for (Node node : nodeList) {
                    printNode(0, node, writer);
                    writer.write("\n");
                }
    
                writer.close();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("nodeList:" + nodeList);
    }
}
